package com.shou.types.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:TimeUtil
 * Project:SmartPark
 * Package: com.shou.types.utils
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/21 20:50
 * @Version 1.0
 */

public class TimeUtil {
    public static DateTimeFormatter formatterDay = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static DateTimeFormatter formatterMonth = DateTimeFormatter.ofPattern("yyyy-MM");
    public static DateTimeFormatter formatterYear = DateTimeFormatter.ofPattern("yyyy");
    public static DateTimeFormatter formatterHour = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static List<String> getTimeUnits(LocalDateTime startTime, LocalDateTime endTime, String timeUnit) {
        List<String> result = new ArrayList<>();

        switch (timeUnit) {
            case "year":
                for (int year = startTime.getYear(); year <= endTime.getYear(); year++) {
                    result.add(String.valueOf(year));
                }
                break;
            case "month":
                LocalDateTime current = startTime.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
                while (!current.isAfter(endTime)) {
                    result.add(current.format(formatterMonth));
                    current = current.plusMonths(1);
                    // Adjust day to 1st of the month in case of varying month lengths
                    current = current.withDayOfMonth(1);
                }
                break;
            case "day":
                current = startTime.toLocalDate().atStartOfDay();
                while (!current.isAfter(endTime)) {
                    result.add(current.format(formatterDay));
                    current = current.plusDays(1);
                }
                break;
            case "hour":
                current = startTime;
                while (!current.isAfter(endTime)) {
                    result.add(current.format(formatterHour));
                    current = current.plusHours(1);
                }
                break;
            default:
                throw new IllegalArgumentException("Unsupported time unit: " + timeUnit);
        }

        return result;
    }
    public static List<String[]> getTimeRangesInxAxisData(String lotCode, List<String> xAxisData, String timeUnit) {
        List<String[]> timeRanges = new ArrayList<>();

        xAxisData.forEach(dateStr -> {
            switch (timeUnit) {
                case "day":
                    LocalDateTime startOfDay = LocalDateTime.parse(dateStr + " 00:00:00", formatterHour);
                    LocalDateTime endOfDay = startOfDay.plusDays(1).minusSeconds(1);
                    timeRanges.add(new String[]{startOfDay.format(formatterHour), endOfDay.format(formatterHour)});
                    break;
                case "month":
                    int year = Integer.parseInt(dateStr.substring(0, 4));
                    int month = Integer.parseInt(dateStr.substring(5));
                    LocalDateTime startOfMonth = LocalDateTime.of(year, month, 1, 0, 0);
                    LocalDateTime endOfMonth = startOfMonth.plusMonths(1).minusSeconds(1);
                    timeRanges.add(new String[]{startOfMonth.format(formatterHour), endOfMonth.format(formatterHour)});
                    break;
                case "year":
                    LocalDateTime startOfYear = LocalDateTime.of(Integer.parseInt(dateStr), 1, 1, 0, 0);
                    LocalDateTime endOfYear = LocalDateTime.of(Integer.parseInt(dateStr), 12, 31, 23, 59, 59);
                    timeRanges.add(new String[]{startOfYear.format(formatterHour), endOfYear.format(formatterHour)});
                    break;
                case "hour":
                    LocalDateTime startTime = LocalDateTime.parse(dateStr, formatterHour);
                    LocalDateTime endTime = startTime.plusHours(1).minusSeconds(1);
                    timeRanges.add(new String[]{startTime.format(formatterHour), endTime.format(formatterHour)});
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported time unit: " + timeUnit);
            }
        });
        return timeRanges;
    }
    public static String getTimeCron(LocalDateTime time) {
        return String.join(
                " ",
                String.valueOf(time.getSecond()),
                String.valueOf(time.getMinute()),
                String.valueOf(time.getHour()),
                String.valueOf(time.getDayOfMonth()),
                String.valueOf(time.getMonthValue()),
                "?",
                String.valueOf(time.getYear()) + "-" + String.valueOf(time.getYear()));
    }

        public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.parse("2025-02-21T01:00:00");
        LocalDateTime endTime = LocalDateTime.parse("2025-03-01T02:00:00");
        DateTimeFormatter formatterDay = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        System.out.println(getTimeUnits(startTime, endTime, "日"));
//        System.out.println(getTimeUnits(startTime, endTime, "月"));
//        System.out.println(getTimeUnits(startTime, endTime, "时"));
//        System.out.println(getTimeUnits(startTime, endTime, "年"));
        LocalDateTime.parse("2025-02-21 01:00:00", formatterDay);
    }
}
